package com.pic.mgt.syushin.converter;

import java.util.Date;

import com.pic.mgt.monitor.BusinessUnitConverter;
import com.pic.mgt.monitor.WrongUnitFormatException;
import com.pic.mgt.monitor.parser.FormatParser;
import com.pic.mgt.syushin.unit.SyushinBusinessUnit;
import com.pic.mgt.unit.Business;

public abstract class SyushinBusinessUnitConverter extends BusinessUnitConverter {
	private final int filesizeLineNo = 1; // 目標檔案大小:確認檔內容的第 1 行
	
	private FormatParser uploadTimeParser;
	private FormatParser terminalIDParser;
	private FormatParser filesizeParser;
	
	private SyushinBusinessUnit syushinBusinessUnit;

	@Override
	protected void parseIGXTypeContent(Business business)
						throws WrongUnitFormatException {
		this.syushinBusinessUnit = (SyushinBusinessUnit)business;
		
		// 分析並填入配信內容 (第三層級)
		doFill();
		
		// 檢查 Target filesize
		// WrongUnitFormatException
		boolean matched = 
				super.checkTargetFilesizeMatched(
						this.syushinBusinessUnit.getBusinessFilesize());
		if (!matched) {
			throw new WrongUnitFormatException(
			"The specified filesize is not equal to the filesize of target file.");
		}
		
		// 分析並填入配信業務內容 (第四層級) ---> 預留
		parseBizTypeContent(this.syushinBusinessUnit);
	}
	
	/*
	 * 放入集信內容資訊:
	 * (1) Filesize
	 * (2) Terminal ID
	 * (3) Upload Date
	 */
	private void doFill() throws WrongUnitFormatException {
		// --------------------- (1) File size ---------------------
		fillFilesize();
		
		// --------------------- (2) Terminal ID ---------------------
		fillTerminalID();
		
		// --------------------- (3) Upload Date ---------------------
		fillUploadTime();
	}
	
	private void fillFilesize() throws WrongUnitFormatException {
		try {			
			// throws IndexOutOfBoundsException
			String filesizeStr = super.
					getConfirmationContentAtLine(this.filesizeLineNo - 1);
			Long filesize = (Long)this.filesizeParser.parse(filesizeStr);
			this.syushinBusinessUnit.setBusinessFilesize(filesize);
		} catch (IndexOutOfBoundsException e) {			
			throw new WrongUnitFormatException(
					"Can't find file size at line " + this.filesizeLineNo, e);
		}
	}
	
	private void fillUploadTime() throws
								WrongUnitFormatException {
		Date uploadTime = (Date)
							this.uploadTimeParser.parse(
									this.confirmationFile.getName());
		this.syushinBusinessUnit.setUploadTime(uploadTime);
	}
	
	private void fillTerminalID()
						throws WrongUnitFormatException {
		String terminalID = (String)this.terminalIDParser.
									parse(this.confirmationFile.getName());
		this.syushinBusinessUnit.setTerminalID(terminalID);
	}
	
	// 分析並填入集信業務內容 (第三層) ---> 預留
	abstract void parseBizTypeContent(SyushinBusinessUnit syushinBusinessUnit)
							throws WrongUnitFormatException;
	
	// --- getter and setter ---
	
	public void setUploadTimeParser(FormatParser uploadTimeParser) {
		this.uploadTimeParser = uploadTimeParser;
	}

	public void setTerminalIDParser(FormatParser terminalIDParser) {
		this.terminalIDParser = terminalIDParser;
	}

	public void setFilesizeParser(FormatParser filesizeParser) {
		this.filesizeParser = filesizeParser;
	}
}
